-- FormBase
-- create by zouyb
-- 公共窗口的一些函数

--初始化公共道具组件
function initItemIcon2(itemNode, data)
    --星星先隐藏
    local stars = {};
    for i = 1, 5 do
        local star = findChildByName(itemNode, "star"..i);
        star = replaceImageView(star);
        star:loadTexture("images/ui/bag/star_grey.png");
        star:setVisible(false);
        table.insert(stars, star);
    end

    --数量也隐藏
    local textBg = findChildByName(itemNode, "text_bg");
    textBg:setVisible(false);
    local text = findChildByName(itemNode, "text");
    text:setVisible(false);

    if data == nil then
        return;
    end

    --加载图标
    local itemClassId = data[2];
    local icon = findChildByName(itemNode, "icon");
    icon = replaceImageView(icon);
    icon:loadTexture(getItemIconPath(ItemM.query(itemClassId, "icon")));
    --点击后可显示道具具体信息
    icon:setTouchEnabled(true);
    registerItemClickEvent(icon, itemClassId);

    --显示数量
    local mask = findChildByName(itemNode, "mask");
    local curNum = ItemM.getAmount(ME.user, itemClassId);
    text:setString(curNum .. "/" .. data[3]);
    if curNum < data[3] then
        -- 数量不足时，数量用红色显示，并且显示灰色遮罩
        text:setTextColor(TextStyleM.TEXT_COLOR_DARK_RED);
        mask:setVisible(true);
    else
        text:setTextColor(TextStyleM.TEXT_COLOR_GRAY);
        mask:setVisible(false);
    end
    text:setVisible(true);
    textBg:setVisible(true);

    --显示实际星星数
    for i = 1, 5 do
        stars[i]:setVisible(true);
    end

    local star = tonumber(ItemM.query(itemClassId, "rarity"));
    for i = 1, star do
        stars[i]:loadTexture("images/ui/bag/start_yellow.png");
    end
end

--根据消耗列表，初始化公共消耗窗口组件
function initComsumeForm(formRoot, cost)
    --先隐藏星星和数量
    for i = 1, 5 do
        local itemNode = findChildByName(formRoot, "root/bottom_part/item"..i);
        initItemIcon2(itemNode, nil);
    end

    --再根据消耗初始化
    local itemPos = 0;
    for i = 1, #cost do
        local data = cost[i];
        if data[1] == 1 then -- 物品
            itemPos = itemPos + 1;
            local itemNode = findChildByName(formRoot, "root/bottom_part/item"..itemPos);
            initItemIcon2(itemNode, data);
        else --货币
            if data[2] == "money" then
                local money = findChildByName(formRoot, "root/bottom_part/money");
                money:setString(tostring(data[3]));
                local curMoney = ME.user.dbase:query("money", 0);
                if curMoney < data[3] then
                    money:setTextColor(TextStyleM.TEXT_COLOR_DARK_RED);
                else
                    money:setTextColor(TextStyleM.TEXT_COLOR_BROWN_YELLOW);
                end
            end
        end
    end
end

-------------------------------
-- 将窗口变暗
-- @param formNode     窗口节点
-- @param flag         true变暗     false还原
-- @param exceptList   例外的组件路径
function turnFormDark(formNode, flag, exceptList)
--    local setColor = iif(flag, TextStyleM.TEXT_COLOR_BLEND, TextStyleM.TEXT_COLOR_NORMAL);
--    local unsetColor = iif(flag, TextStyleM.TEXT_COLOR_NORMAL, TextStyleM.TEXT_COLOR_BLEND);

    local setFlag = iif(flag, true, false);
    local unsetFlag = not setFlag;

    -- 获取目标节点
    local exceptNodes = {};
    for _, path in pairs(exceptList) do
        local child = findChildByName(formNode.node, path);
        table.insert(exceptNodes, child);
    end

    -- 获取一个节点的所有父节点
    local function getParentList(node)
        local list = {};
        if node == formNode then
            return list;
        end

        local tmpNode = node:getParent();
        while tmpNode ~= formNode do
            table.insert(list, tmpNode);
            tmpNode = tmpNode:getParent();
        end
        return list;
    end

    -- 获取目标节点的所有父节点
    local parentList = {};
    for i = 1, #exceptNodes do
        parentList = table.append(parentList, { exceptNodes[i] });
        parentList = table.append(parentList, getParentList(exceptNodes[i]));
    end

    -- 判断是否目标节点
    local function isInExceptList(child)
        for _, tempChild in pairs(exceptNodes) do
            if tempChild == child then
                return true;
            end
        end

        return false;
    end

    -- 判断是否是目标节点的父节点
    local function isInParentList(child)
        for _, tempChild in pairs(parentList) do
            if tempChild == child then
                return true;
            end
        end

        return false;
    end

    local function setChildColor(node)
        local children = node:getChildren();
        local len = table.getn(children)
        for i = 1, len do
            local child = children[i];
            if not isInParentList(child) then
                setDarkMode(child, setFlag);
            else
                setDarkMode(child, unsetFlag);
            end

            if not isInExceptList(child) then
                -- 嵌套处理子节点
                setChildColor(child);
            end
        end
    end

    setChildColor(formNode);
end


-------------------------------
-- 将窗口变灰
-- @param formNode     窗口节点
-- @param flag         true变灰     false还原
-- @param exceptList   例外的组件路径
function turnFormGray(formNode, flag, exceptList)
    local setFlag = iif(flag, true, false);
    local unsetFlag = not setFlag;

    -- 获取目标节点
    local exceptNodes = {};
    if type(exceptList) == table then
        for _, path in pairs(exceptList) do
            local child = findChildByName(formNode.node, path);
            table.insert(exceptNodes, child);
        end
    end

    -- 获取一个节点的所有父节点
    local function getParentList(node)
        local list = {};
        if node == formNode then
            return list;
        end

        local tmpNode = node:getParent();
        while tmpNode ~= formNode do
            table.insert(list, tmpNode);
            tmpNode = tmpNode:getParent();
        end
        return list;
    end

    -- 获取目标节点的所有父节点
    local parentList = {};
    for i = 1, #exceptNodes do
        parentList = table.append(parentList, { exceptNodes[i] });
        parentList = table.append(parentList, getParentList(exceptNodes[i]));
    end

    -- 判断是否目标节点
    local function isInExceptList(child)
        for _, tempChild in pairs(exceptNodes) do
            if tempChild == child then
                return true;
            end
        end

        return false;
    end

    -- 判断是否是目标节点的父节点
    local function isInParentList(child)
        for _, tempChild in pairs(parentList) do
            if tempChild == child then
                return true;
            end
        end

        return false;
    end

    local function setChildColor(node)
        local children = node:getChildren();
        local len = table.getn(children)
        for i = 1, len do
            local child = children[i];
            if not isInParentList(child) then
                setGrayMode(child, setFlag);
            else
                setGrayMode(child, unsetFlag);
            end

            if not isInExceptList(child) then
                -- 嵌套处理子节点
                setChildColor(child);
            end
        end
    end

    setChildColor(formNode);
end

-- 关闭窗口
function closeFormByName(formName)
    UIMgr.getCurrentScene():removeFormByName(formName);
end

-- back键点击后退出游戏处理
function exitGamePrompt(form)
    local tickNode = cc.Node:create();
    form:addChild(tickNode);

    local function onBackEvent()
        -- 如果是雷霆平台，并且无需弹出游戏确认框
        if PUBLISH_PLATFORM == PUBLISH_TYPE_LT then
            if LeiTingAgent and LeiTingAgent.isConfirmExit and not LeiTingAgent.isConfirmExit() then
                exitSystem();
                return;
            end
        end

        -- 如果是UC平台，直接退出
        if PUBLISH_PLATFORM == PUBLISH_TYPE_UC then
            if UCAgent then
                exitSystem();
                return;
            end
        end

        local function okFunc()
            -- 退出游戏
            exitSystem();
        end

        confirm2(CONFIRM_TYPE_OK_CANCEL, getLocStr("exit_game_title"), getLocStr("exit_game_confirm"), okFunc);
    end

    AddBackEventHandler(form, nil, onBackEvent);
end

